package com.hyj.jpdy.job.RunModle;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hyj.jpdy.bean.vo.Article;
import com.hyj.jpdy.grap.rss.RssUtil;
import com.hyj.jpdy.grap.rss.bean.Item;
import com.hyj.jpdy.grap.rss.bean.RssChannel;
import com.hyj.jpdy.service.IGrapService;
import com.hyj.util.HttpUtil;

public class RssModle implements Runnable {

	
	private IGrapService grapService;
	Log log = LogFactory.getLog(RssModle.class);
	
	public RssModle(IGrapService grapService){
		this.grapService = grapService;
	}
	
	@Override
	public void run() {
		String url = "http://www.alibuybuy.com/feed";
		String content = "";
		try {
			content = HttpUtil.getRemoteResp(url,"UTF-8");
		} catch (IOException e) {
			log.error("", e);
		}
		RssChannel rss = RssUtil.parserRss(content);
		String title = rss.getTitle();
		log.info("栏目名称："+title);
		List<Item> list = rss.getItems();
		
		for(Item i:list){
			try {
				Article item = new Article();
				item.setTypeId(0000001);
				item.setColumnId(20);
				item.setTitle(i.getTitle());
				item.setFromUrl(i.getLink());
				item.setColumnName(title);
				SimpleDateFormat sdf = new SimpleDateFormat("EEE,dd MMM yyyy HH:mm:ss Z", new Locale("ENGLISH", "CHINA"));
				  Date d =  sdf.parse(i.getPubDate());
				  sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				item.setPubDate(sdf.format(d));
				item.setContent(i.getDescription());
				item.setItemID("测试RSS笑话");
				grapService.saveItemInfo(item);
			} catch (Exception e) {
				log.error("", e);
			}
		}

	}

}
